package com.qf.agro.controller;

import com.qf.agro.common.ResultInfo;
import com.qf.agro.common.SysUserHolder;
import com.qf.agro.pojo.SysUser;
import com.qf.agro.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author 10202
 * @version V1.0
 * @Project smart-agriculture-parent
 * @Package com.qf.agro.controller
 * @Date 2023/5/20 16:34
 */
@RestController
public class SysUserController {
    private final SysUserService sysUserService;

    public SysUserController(SysUserService sysUserService) {
        this.sysUserService = sysUserService;
    }

    @RequestMapping("/sys/user/login")
    public ResultInfo login(@RequestBody SysUser sysUser)
    {
        return sysUserService.login(sysUser.getUsername(),sysUser.getPassword());
    }

    @GetMapping("/sys/user/page")
    public ResultInfo getPage(@RequestParam(value = "page",defaultValue ="1") Long pageNum,
    @RequestParam(value = "pageSize",defaultValue ="5") Long pageSize ,
    @RequestParam(value = "name",defaultValue ="") String name
    ){
        System.out.println(pageNum);
        System.out.println(pageSize);
        return sysUserService.getPage(pageNum,pageSize,name);
    }


    @DeleteMapping("sys/user")
    public ResultInfo deleteUser1(Integer id){
        System.out.println(id);
        return sysUserService.deleteUser(id);
    }

    @PutMapping("sys/user")
    public ResultInfo updateMapping(@RequestBody SysUser sysUser){

        return sysUserService.updateUser(sysUser);
    }

    @PostMapping("sys/user/checkUsername")
    public ResultInfo checkUsername(@RequestBody Map<String,Object> map){
        String username = (String) map.get("username");

        return sysUserService.checkUsername(username);

    }

    @PostMapping("sys/user")
    public ResultInfo insertUser(@RequestBody SysUser sysUser){
        sysUser.setState(1);
        return sysUserService.insertUser(sysUser);

    }


    @PostMapping("/sys/user/logout")
    public ResultInfo logout(@RequestBody SysUser sysUser){

        SysUserHolder.remove();
        return ResultInfo.success("退出成功");
    }
}
